<template>
	<div class="right-box">
		<Breadcrumb style="margin-bottom: 5px;">
	        <BreadcrumbItem>
	            <Icon type="ios-paper" color="red" size="18"></Icon> 基本资料
	        </BreadcrumbItem>
	        <BreadcrumbItem>
	            供应商管理
	        </BreadcrumbItem>
	    </Breadcrumb>
	    <Input search enter-button="搜索" placeholder="请输入供应商名字..." v-model="search" class="search-input" @on-search="searchSupliers"/>
	    <Button class="addBtn" @click="addSuplier" v-if="userType==0">新增供应商</Button>
	    <Table height="480" :columns="columns1" :data="datas">
	    	<template slot-scope="{row,index}" slot="rep">
	    		<span v-show="row.supplierRep==3">极好</span>
	    		<span v-show="row.supplierRep==2">满意</span>
	    		<span v-show="row.supplierRep==1">良好</span>
	    		<span v-show="row.supplierRep < 1">恶劣</span>
	    	</template>
	    	 <template slot-scope="{ row, index }" slot="action">
	            <Button type="primary" style="margin-right: 5px" @click="edit(row)" v-if="userType == 0">修改</Button>
	            <Button type="error" @click="remove(row)">删除</Button>
	        </template>
	    </Table>
	    <Modal
        title="请按要求完善表单"
        v-model="modelEdit"
        :closable="false" @on-ok="checkSure" @on-cancel="handleReset('formValidate')" :styles="{top: '20px'}">
	        <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100">
		        <FormItem label="供应商名称" prop="name">
		            <Input v-model="formValidate.name" placeholder="请输入供应商名称"></Input>
		        </FormItem>
		        <FormItem label="供应商信誉" prop="level">
		            <Select v-model="formValidate.level">
		                <Option value="3">极好</Option>
		                <Option value="2">满意</Option>
		                <Option value="1">良好</Option>
		                <Option value="-1">恶劣</Option>
		            </Select>
		        </FormItem>
		    </Form>
	    </Modal>
	    <Modal
        title="请按要求完善表单"
        v-model="modelShow"
        :closable="false" @on-ok="checkSure1" @on-cancel="handleReset('formValidate1')" :styles="{top: '20px'}">
	        <Form ref="formValidate1" :model="formValidate1" :rules="ruleValidate1" :label-width="100">
		        <FormItem label="供应商名称" prop="name">
		            <Input v-model="formValidate1.name" placeholder="请输入供应商名称"></Input>
		        </FormItem>
		        <!--<FormItem label="供应商信誉" prop="level">
		            <Select v-model="formValidate1.level">
		                <Option value="3">极好</Option>
		                <Option value="2">满意</Option>
		                <Option value="1">良好</Option>
		                <Option value="-1">恶劣</Option>
		            </Select>
		        </FormItem>-->
		    </Form>
	    </Modal>
	</div>
</template>

<script>
	export default{
		name:'suplierManager',
		data(){
			return{
				search:'',
				modelShow:false,
				modelEdit:false,
				formValidate: {
                    name: '',
                    level:''
                },
                ruleValidate: {//表单校验规则
                    name: [
                        { required: true, message: '供应商名字不能为空', trigger: 'blur' }
                    ],
                    level: [
                        { required: true, message: '请选择供应商信誉', trigger: 'blur' }
                    ]
                },
                formValidate1: {
                    name: '',
                    level:''
                },
                ruleValidate1: {//表单校验规则
                    name: [
                        { required: true, message: '供应商名字不能为空', trigger: 'blur' }
                    ],
                    level: [
                        { required: true, message: '请选择供应商信誉', trigger: 'blur' }
                    ]
                },
				columns1: [//表头
                    {
                        title: '供应商编号',
                        key: 'supplierCode'
                    },
                    {
                        title: '供应商名称',
                        key: 'supplierName'
                    },
                    {
                        title: '供应商信誉',
                        key: 'supplierRep',
                        slot:'rep'
                    },
                    {
                        title: '供货次数',
                        key: 'supplierCount'
                    },
                    {
                        title: '创建时间',
                        key: 'createTime'
                    },{
                    	title:'操作',
                        slot: 'action'
                    }
                ],
				datas:[],//表格数据
				currentRow:null,
				userType:0
			}
		},
		methods:{
			addSuplier(){//新增供应商
				this.modelShow = true
			},
            handleReset (name) {
                this.$refs[name].resetFields();
            },
            handleSubmit(name) {//验证表单
               this.$refs[name].validate((valid) => {
                    if (valid) {//验证通过
                    	if(name=="formValidate"){
                    		this.editThisSuplier()
                    	}else{
                    		this.addThisSuplier()
                    	}
                    } else {//验证不通过
                        this.$Message.error('请按要求完善表单!');
                        this.handleReset('formValidate')
                    }
                })
            },
			checkSure(){//点击弹窗确定 验证表单
				this.handleSubmit('formValidate')
			},
			checkSure1(){//点击弹窗确定 验证表单
				this.handleSubmit('formValidate1')
			},
			edit(row){//修改供应商信息
				this.modelEdit = true
				this.currentRow = row
				this.formValidate.name = row.supplierName
			},
			addThisSuplier(){//新增
				let that = this
				this.$axios({
	    			method:'post',
	    			url:that.GLOBAL.url+'/sys/addSupplier',
	    			data:{
	    				supplierName:that.formValidate1.name,
	    				supplierRep:that.formValidate1.level
	    			},
	    			transformRequest:[function(data){
	    				let ret = ''
	    				for(let it in data){
	    					ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
	    				}
	    				return ret
	    			}],
	    			xhrFields:{
	    				widthCredentials:true
	    			},
	    			crossDomain:true,
	    			header:{
	    				'Content-Type': 'application/x-www-form-urlencoded'
	    			}
	    		}).then(res=>{
	    			console.log(res)
	    			if(res.data.code == 200){
	    				this.$Message.success(res.data.msg);
	    				this.getSupliers()
	    			}else{
	    				this.$Message.error(res.data.msg);
	    			}
	    		}).catch(function(err){
	    			console.log(err)
	    		})
			},
			editThisSuplier(){//编辑
				let that = this
				this.$axios({
	    			method:'post',
	    			url:that.GLOBAL.url+'/sys/modifySupplier',
	    			data:{
	    				supplierId:that.currentRow.supplierId,
	    				supplierRep:that.formValidate.level
	    			},
	    			transformRequest:[function(data){
	    				let ret = ''
	    				for(let it in data){
	    					ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
	    				}
	    				return ret
	    			}],
	    			xhrFields:{
	    				widthCredentials:true
	    			},
	    			crossDomain:true,
	    			header:{
	    				'Content-Type': 'application/x-www-form-urlencoded'
	    			}
	    		}).then(res=>{
	    			console.log(res)
	    			if(res.data.code == 200){
	    				this.$Message.success(res.data.msg);
	    				this.getSupliers()
	    			}else{
	    				this.$Message.error(res.data.msg);
	    			}
	    		}).catch(function(err){
	    			console.log(err)
	    		})
			},
			remove(row){//删除供应商
				let that = this
				this.$axios({
	    			method:'post',
	    			url:that.GLOBAL.url+'/sys/deleteSupplier',
	    			data:{
	    				supplierId:row.supplierId
	    			},
	    			transformRequest:[function(data){
	    				let ret = ''
	    				for(let it in data){
	    					ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
	    				}
	    				return ret
	    			}],
	    			xhrFields:{
	    				widthCredentials:true
	    			},
	    			crossDomain:true,
	    			header:{
	    				'Content-Type': 'application/x-www-form-urlencoded'
	    			}
	    		}).then(res=>{
	    			console.log(res)
	    			if(res.data.code == 200){
	    				this.$Message.success(res.data.msg);
	    				this.getSupliers()
	    			}else{
	    				this.$Message.error(res.data.msg);
	    			}
	    		}).catch(function(err){
	    			console.log(err)
	    		})
			},
			searchSupliers(){//搜索
				if(this.search==''){this.getSupliers();return;}
				let that = this
				this.$axios({
	    			method:'post',
	    			url:that.GLOBAL.url+'/sys/selectSupplierName',
	    			data:{
	    				supplierName:that.search
	    			},
	    			transformRequest:[function(data){
	    				let ret = ''
	    				for(let it in data){
	    					ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
	    				}
	    				return ret
	    			}],
	    			xhrFields:{
	    				widthCredentials:true
	    			},
	    			crossDomain:true,
	    			header:{
	    				'Content-Type': 'application/x-www-form-urlencoded'
	    			}
	    		}).then(res=>{
	    			console.log(res)
	    			if(res.data.code == 200){
	    				this.$Message.success(res.data.msg);
	    				this.datas = res.data.supplierList
	    			}else{
	    				this.$Message.error(res.data.msg);
	    			}
	    		}).catch(function(err){
	    			console.log(err)
	    		})
			},
			getSupliers(){ // 获取供应商列表
				let that = this
				this.$axios({
	    			method:'post',
	    			url:that.GLOBAL.url+'/sys/getSupplier',
	    			transformRequest:[function(data){
	    				let ret = ''
	    				for(let it in data){
	    					ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
	    				}
	    				return ret
	    			}],
	    			xhrFields:{
	    				widthCredentials:true
	    			},
	    			crossDomain:true,
	    			header:{
	    				'Content-Type': 'application/x-www-form-urlencoded'
	    			}
	    		}).then(res=>{
	    			console.log(res)
	    			if(res.data.code == 200){
	    				this.datas = res.data.supplierList
	    			}else{
	    				this.$Message.error(res.data.msg);
	    			}
	    		}).catch(function(err){
	    			console.log(err)
	    		})
			}
		},
		created(){
			this.userType=this.LS.getLS('userType')
			this.getSupliers()
		}
	}
</script>

<style scoped>
.right-box{
	width: 100%;
	height: 100%;
	position: relative;
}
.addBtn{
	position: absolute;
	top: -10px;
	right: 20px;
}
.page-nav{
	width: 100%;
	display: -webkit-flex;
	display: -ms-flexbox;
	display: -moz-box;
	display: flex;
	justify-content: center;
	align-content: center;
	margin-top: 10px;
}
.search-input{
	width:40%;
	position: absolute;
	top: -10px;
	left: 50%;
	transform: translateX(-50%);
	-webkit-transform: translateX(-50%);
}
</style>